package service;

import dao.GradeMapper;
import dao.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Grade;
import pojo.Memberinfo;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private GradeMapper gradeMapper;
    private MemberMapper memberMapper;

    public void setMemberMapper(MemberMapper memberMapper) {
        this.memberMapper = memberMapper;
    }

    public Memberinfo login(String name, String password) {
        return memberMapper.login(name,password);
    }

    public List<Memberinfo> allMember() {
        return memberMapper.allMember();
    }

    public List<Memberinfo> findMemberBySearch(String mname, int typeId) {
        String sql="";
        List<String> list = new ArrayList<String>();
        if(!"".equals(mname)){
            list.add(" name like '%"+mname+"%' ");
        }
        if(typeId!=0){
            list.add(" grade_id="+typeId+"");
        }
        if(list.size()!=0){
            sql+="where ";
            for (String s:list){
                sql+=s+" and ";
            }
            sql = sql.substring(0,sql.length()-4);
        }
       /* System.out.println(sql);
        System.out.println(sql);
        System.out.println(sql);*/
        List<Memberinfo> List2 = memberMapper.findMemberBySearch(sql);
        if(List2==null){
            return null;
        }
        for (Memberinfo mi:List2){
            int id = mi.getGradeId();
            Grade grade = gradeMapper.findGradeById(id);
            mi.setGrade(grade);
        }
        return List2;
    }
    //分页查询实现
    public List<Memberinfo> selectPage(int start, int size) {
        return memberMapper.selectPage(start,size);
    }
    //  总数据数
    public int selectCount() {
        return memberMapper.selectCount();
    }

    public Memberinfo findById(int id) {
        Memberinfo memberinfo = memberMapper.findById(id);
        int gradeId = memberinfo.getGradeId();
        Grade grade = gradeMapper.findGradeById(gradeId);
        memberinfo.setGrade(grade);

        return memberinfo;
    }

    public int update(Memberinfo memberinfo) {
        int update = memberMapper.update(memberinfo);
        return 0;
    }

    public int save(Memberinfo memberinfo,int typeId) {
        memberinfo.setGradeId(typeId);
        int list = memberMapper.save(memberinfo);
        return list;
    }


    public int deleteMwmber(int id) {
        return memberMapper.deleteMwmber(id);
    }

    public int deltteAll(Integer[] ids) {
        return  memberMapper.deltteAll(ids);
    }
}
